/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.scroller {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.display.ui.scroller.AbstractMCScroler;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MCScrollerWithUpDnButtons extends AbstractMCScroler implements IMLBroadcasterListener {

		private var slider:MCScrollerWithConstantLengthSlider;
		private var btnDN:MLButton;
		private var btnUP:MLButton;
		private var btnUPLength:Number;
		private var btnDNLength:Number;		public var isInvisibleWhenNoScroll:Boolean=false;

		public function MCScrollerWithUpDnButtons(mc:MovieClip, isConstantLenthSlider:Boolean) {
			super(mc);
			if(isConstantLenthSlider){
				slider=new MCScrollerWithConstantLengthSlider(mlmc.getMC('slider'));
			}else{
				slider=new MCScroller(mlmc.getMC('slider'));
			}
			btnUP=mlmc.getMLButton('btnUP');
			btnUP.addListener(this);
			btnDN=mlmc.getMLButton('btnDN');
			btnDN.addListener(this);
			
			btnUPLength=btnUP.btn.height;
			btnDNLength=btnUP.btn.height;
			
			var orgRotation:Number=mc.rotation;
			mc.rotation=0;
			setLength(mc.height);
			mc.rotation=orgRotation;
			mc.scaleY=1;
			
			addEventsMapping(slider);
		}
		
		override public function setLength(newLength:Number):void {
			newLength=Math.max(btnDNLength+btnUPLength+12, newLength);
			slider.setLength(newLength-btnDNLength-btnUPLength);
			btnDN.btn.y=newLength-btnDNLength;
		}
		
		override public function getLength():Number {
			return btnDN.btn.y+btnDNLength;
		}
		
		/**
		 * Sets the slider in position
		 * 
		 * @param position number from 0 to 1
		 * @param visibleToTotal dependence of visible content (eg. text) to total length (visible/total)
		 */
		override public function setPosition(position:Number, visibleToTotal:Number):void {
			slider.setPosition(position, visibleToTotal);
			mc.visible=!isInvisibleWhenNoScroll || visibleToTotal<1;
		}
		
		/**
		 * @return position number from 0 to 1 (determined by slider position)
		 */
		override public function getPosition():Number {
			return slider.getPosition();
		}
		
		override public function getVisibleToTotal():Number {
			return slider.getVisibleToTotal();
		}
		
		override public function setScrolled(scrolled:IScrollable):void {
			super.setScrolled(scrolled);
			slider.setScrolled(scrolled);
		}
		
		override public function unsetScrolled():void {
			super.unsetScrolled();			slider.unsetScrolled();
		}
		
//********************************************************************************************
//		events for MCScrollerWithUpDnButtons
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case btnDN:
					switch(ev.event) {
						case btnDN.event_Press:
							if(!scrolled){ break; }
							scrolled.setScrollByOneUnit(true);
						break;
					}
				break;
				case btnUP:
					switch(ev.event) {
						case btnUP.event_Press:
							if(!scrolled){ break; }
							scrolled.setScrollByOneUnit(false);
						break;
					}
				break;
			}
		}
		
	}
}
